package com.example.add8_17;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.add8_17.adapter.VpFrgmentAdapter;
import com.example.add8_17.fragment.HomeFragment;
import com.example.add8_17.fragment.SqlFragment;
import com.example.add8_17.fragment.WoFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    private TextView mBarText;
    private Toolbar mToolbar;
    private ViewPager mVp;
    private TabLayout mTab;
    private LinearLayout mLin;
    private NavigationView mNavig;
    private DrawerLayout mDrawer;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initViews();
    }

    private void initViews() {
        mBarText = findViewById(R.id.bar_text);
        mToolbar = findViewById(R.id.toolbar);
        mVp = findViewById(R.id.vp);
        mTab = findViewById(R.id.tab);
        mLin = findViewById(R.id.lin);
        mNavig = findViewById(R.id.navig);
        mDrawer = findViewById(R.id.drawer);
        inittoolbar();
        inittab();
        initnavig();
    }

    private void initnavig() {
        View headerView = mNavig.getHeaderView(0);
        image = headerView.findViewById(R.id.image_header);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View inflate = LayoutInflater.from(Main2Activity.this).inflate(R.layout.header_pop, null);
                Button xj = inflate.findViewById(R.id.xj);
                Button xc=inflate.findViewById(R.id.xc);
                PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setBackgroundDrawable(new ColorDrawable());
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(image,0,0);
                xj.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initxj();
                    }
                });
                xc.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initjc();
                    }
                });
            }
        });
        initmenu();
    }

    private void initmenu() {
        mNavig.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.xiazai:
                        startActivity(new Intent(Main2Activity.this,XiazaiActivity.class));
                        break;
                    case R.id.navi:
                        startActivity(new Intent(Main2Activity.this,NaviActivity.class));
                        break;
                    case R.id.tupian:
                        break;
                }
                return false;
            }
        });
    }

    private void initjc() {
        Intent intent = new Intent(Intent.ACTION_PICK,null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        startActivityForResult(intent,200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200){
            Uri data1 = data.getData();
            Glide.with(this).load(data1).into(image);
        }else if (requestCode==100){

        }
    }

    private void initxj() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,100);
    }

    private void inittab() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new SqlFragment());
        fragments.add(new WoFragment());
        VpFrgmentAdapter vpFrgmentAdapter = new VpFrgmentAdapter(getSupportFragmentManager(), fragments);
        mVp.setAdapter(vpFrgmentAdapter);
        mTab.setupWithViewPager(mVp);
        mBarText.setText("首页");
        mTab.getTabAt(0).setText("首页");
        mTab.getTabAt(1).setText("收藏");
        mTab.getTabAt(2).setText("我的");
        mTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 0:
                        mBarText.setText("首页");
                        break;
                    case 1:
                        mBarText.setText("收藏");
                        break;
                    case 2:
                        mBarText.setText("我的");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void inittoolbar() {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.syncState();
        mDrawer.addDrawerListener(actionBarDrawerToggle);
        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                mLin.setX(drawerView.getWidth()*slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }
}
